package com.huasoft.servlet.file;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huasoft.dao.FileDao;
import com.huasoft.dao.PageCondition;
import com.huasoft.domain.File;

public class ListFileServlet {

	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("listFiles", null);
		String types = request.getParameter("types");
		String title = request.getParameter("title");
		System.out.println(title);
		String levels = request.getParameter("levels");
		System.out.println(levels);
		PageCondition pageCondition = new PageCondition();
		if (request.getParameter("page") == null) {
			pageCondition.setPage(1);
		} else {
			levels = new String(levels.getBytes("iso8859-1"), "gbk");
			types = new String(types.getBytes("iso8859-1"), "gbk");
			Integer page = Integer.valueOf(request.getParameter("page"));
			pageCondition.setPage(page);
		}
		FileDao fileDao = new FileDao();
		List<File> files = fileDao
				.listFile(types, title, levels, pageCondition);
		System.out.println(files.size());
		fileDao.totalPage(types, title, levels, pageCondition);
		request.setAttribute("levels", levels);
		request.setAttribute("types", types);
		request.setAttribute("listFiles", files);
		request.setAttribute("totalPage", pageCondition.getTotalPage());
		request.setAttribute("page", pageCondition.getPage());
		request.getRequestDispatcher("/files/files_list.jsp").forward(request,
				response);
	}
}
